package com.sl.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sl.mapper.UserMapper;
import com.sl.mapper.test1.User1Mapper;
import com.sl.model.User;

@RestController
public class UserController {
	
	//@Autowired
	//private UserMapper userMapper;
	@Autowired
	private User1Mapper userMapper;
	@RequestMapping("/getUsers")
	public PageInfo<User> getUsers(int pageNum, int pageSize) {
		//将参数传给这个方法就可以实现物理分页了，非常简单。
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.getAll();
        return new PageInfo<User>(userList);
	}
	
    @RequestMapping("/getUser")
    public User getUser(Long id) {
    	User user=userMapper.getOne(id);
        return user;
    }
    
    @RequestMapping("/getUserList")
    public List<User> getUserList(String nickName,String userName) {
    	Map<String,Object> map=new HashMap<String,Object>();
    	map.put("userName", userName);
    	map.put("nickName", nickName);
    	List<User> users=userMapper.getUserList(map);
        return users;
    }
    
    @RequestMapping("/add")
    public void save(User user) {
    	userMapper.insert(user);
    }
    
    @RequestMapping(value="update")
    public void update(User user) {
    	userMapper.update(user);
    }
    
    @RequestMapping(value="/delete/{id}")
    public void delete(@PathVariable("id") Long id) {
    	userMapper.delete(id);
    }
    
    @RequestMapping(value="/delete/{ids}")
    public void delete(@PathVariable("ids") String ids) {
    	String[] split = ids.split("_");
    	List<Long> list=new ArrayList<Long>();
    	for (int i = 0; i < split.length; i++) {
			String string = split[i];
			list.add(Long.valueOf(string));
		}
    	userMapper.deletes(list);
    }
}